package org.simplesocket.simplenettycore.session;

import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;

@Data
public class ClientSessionContext {
    
    private String sessionID;
    
    private String clientPubRsa;
    
    private String serverPubRsa;
    
    private String serverPriRsa;
    
    private String bothAes;
    
    public <T extends ClientSessionContext> T transform(Class<T> clazz) {
        // 检查目标类是否是 ClientSessionContext 的子类
        if (!ClientSessionContext.class.isAssignableFrom(clazz)) {
            throw new IllegalArgumentException("Target class '" + clazz.getTypeName()
                    + "' must extends '" + ClientSessionContext.class.getTypeName() + "'");
        }
        
        try {
            // 创建目标类实例
            T instance = clazz.getDeclaredConstructor().newInstance();
            // 复制当前对象的属性到新实例
            BeanUtils.copyProperties(instance,this);
            return instance;
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Target class miss constructor:", e);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Create instance error:", e);
        }
    }
    
}
